#pragma once

#include <QtWidgets/QWidget>
#include <QtWidgets/QVBoxLayout>
#include "bondchart.h"
#include "bondcharttools.h"
#include <qbprotocol/include/SSQBModel.h>
#include <widgets/chart/basechart_layoutitem.h>
#include <widgets/chart/basechart_axis.h>

namespace qb
{
    class BondInterestRateChart
		: public QWidget
		, public BaseChartCustomAxisGrid
		, public BaseChartCustomTextElement
    {
		Q_OBJECT
    public:
		BondInterestRateChart(QWidget* parent = nullptr);
		~BondInterestRateChart();

		enum emWndType {
			wndTp_Null = 0,//暂无数据 不显示Tab，标题为信用利差
			wndTp_Lock,//无权限
			wndTp_Issuer,//只有主体信用利差  不显示Tab
			wndTp_Bond,//只有个券信用利差  不显示Tab
			wndTp_Both,
			wndTp_Max
		};

		enum emDataType {
			data_Null = 0, //无数据
			data_CurYear,//仅今年
			data_YesYear,//仅历史
			data_All,//显示所有数据
		};

		void setBondInfo(const CBondInfo* bondinfo);
		void loadData(xQBIssuerCreditSpreadAck* pIssuer, xQBBondCreditSpreadAck* pBond);
		void createChart();
		void refreshData();
		void setDataType(int type);
		void setWndType(int type);
		void setMarketType(em_BrokerKinds emCurType);
		void setSelect(int select);
		void setLeftDrag(double ratio);
		void setRightDrag(double ratio);
		void setCentralDrag(double ratio1, double ratio2);

		PriceKLineList* GetPriceKLineList();

		BaseChartDragBarElement* dragBar() { return bcde; }

		// BaseChartCustomAxisGrid
		virtual bool drawGridXLines(BaseChartPainter* painter);
		virtual bool drawGridYLines(BaseChartPainter* painter);

		// BaseChartCustomTextElement
		virtual bool customDraw(BaseChartPainter* painter, const QRect& rect);

	signals:
		void mouseDoubleClick(QWidget*);

	protected:
		void inputData(xQBIssuerCreditSpreadAck* pIssuer, xQBBondCreditSpreadAck* pBond);
		void transData();
		void transData(const xQBBondCreditSpreadAckUnit& unt, PriceKLine& kData);
		void GetShowLimit(double fMinXShow, double fMaxXShow, double& fMinYShow, double& fMaxYShow);
		void drawMouseMoveLine(BaseChartPainter* painter);
		int GetKLineIndex(int nIndex, std::list<PriceKLine>::iterator& it);
		QString	GetKLineDateString(std::list<PriceKLine>::iterator& it);
		QString	strInt2DateInTable(UINT nDate);
		QString GetPointTips(int pos);
		void LoadPointRating(int pos, QString& strRate, bool isTips);
		void ReloadDataType(bool curThis, bool curHis, bool bondThis, bool bondHis);
		void GetIssuerName(const QString& issuerCode, QString& issuerName);
		double getXPos_Value(double value);

		void paintEvent(QPaintEvent* event) override;
		void leaveEvent(QEvent* event) override;

		Q_SLOT void onMouseMove(QMouseEvent* event);

	private:
		BaseChartCustomPlot*		customPlot		= nullptr;
		QVBoxLayout*				verticalLayout	= nullptr;
		BaseChartDragBarElement*	bcde			= nullptr;

		CBondInfo*					mBondInfo		= nullptr;
		xQBIssuerCreditSpreadAck*	m_pIssuerData	= nullptr;
		xQBBondCreditSpreadAck*		m_pBondData		= nullptr;
		PriceKLineList				m_DataKLineBroker;//报价，成交日线
		PriceKLineList				m_DataKLineCFETS;
		PriceKLineList				m_DataKLineExchange;
		int							m_nMarketType;
		int							m_nWndType;
		int							m_nDataType;//数据显示类型
		int							m_nBtnSelect;
		int							m_nHotPoint;
		int							m_nBegin;
		int							m_nEnd;
		bool						m_bShowBondRate;/*根据权限7是否显示个券隐含评级的调动*/;//m_bHaveHistory/*是否显示历史数据*/
		std::map<int, xQBBondCreditSpreadAckUnit>  m_mapRating;
		double						m_dMinX;
		double						m_dMaxX;
		double						m_dMinY;
		double						m_dMaxY;
		double						m_dOriginMinX;
		double						m_dOriginMaxX;
		double						m_dDragLeft;
		double						m_dDragRight;
    };
}

